const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page:1,
    limit:10,
    bookbag:'',

    list:[]
  },
  name:function(e){
    console.log(e);
    this.setData({
      bookbag:e.detail.value
    })
    this.getlist()
  },
  delete:function(){
    this.setData({
      bookbag:'',
      page:1
    })
    this.getlist()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    that.getlist()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    var number = that.data.page + 1;
    that.setData({
      page: number
    })
    console.log(that.data.page)
    that.getlist()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getlist: function () {
    wx.showLoading({
      title: '加载中'
    })
    var that = this;
    wx.getStorage({
      key: 'yzuser',
      success: function (res) {
        console.log(res);
        let setdata = {
          token: res.data.token,
          page: that.data.page,
          limit: that.data.limit,
          bookbag:that.data.bookbag
        }
        console.log(setdata);
        wx.request({
          url: app.api + app.yzbookbags,
          method: 'POST',
          data: setdata,
          success(e) {
            wx.hideLoading();
            console.log(e);
            if (e.data.msg == 0) {
              if (that.data.page == 1) {
                that.setData({
                  list: e.data.data.list
                })
                console.log("aaa")
                wx.hideLoading();
                if (!e.data.data.list.length) {
                  wx.showToast({
                    title: '暂无数据',
                    icon: 'none',
                    duration: 2000
                  })
                }

              } else if (e.data.data.list.length) {
                let list = that.data.list
                list = list.concat(e.data.data.list)
                that.setData({
                  list: list
                })
                wx.hideLoading()
              } else {
                wx.hideLoading();
                wx.showToast({
                  title: '没有更多了',
                  icon: 'none',
                  duration: 2000
                })
              }

              // 空的

            } else if (e.data.msg == -1) {
              wx.redirectTo({
                url: '../yzlogin/login'
              })
            } else {
              wx.showToast({
                title: e.data.msg,
                icon: 'none',
                duration: 2000
              })
            }

          }
        })
      },
    })
  },
})